#include <iostream>
#include <vector>
#include <algorithm>
#include "interval_tree.h"
#include "types.h"
#include "scheduler.h"
using namespace std;
#include <cstring>

// 用于加速C++的IO操作
void setup_io() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);
}

int main(int argc, char ** argv) {
    if (argc > 1 && strcmp(argv[1], "-d") == 0) {
        enableLog();
    }
    setup_io();

    long long l, m, n;
    if (!(std::cin >> l >> m >> n)) {
        return 1;
    }

    std::vector<Access> accesses(n);
    for (int i = 0; i < n; ++i) {
        accesses[i].id = i;
        if (!(std::cin >> accesses[i].addr >> accesses[i].size >> accesses[i].timeStart >> accesses[i].timeDuration)) {
            return 1;
        }
    }

    // 创建调度器并执行
    Scheduler scheduler(m, accesses);
    scheduler.run();

    // 获取并打印结果
    auto final_schedule = scheduler.getResult();

    // 最终结果需要严格按时间排序，如果时间相同，按操作类型排序以保持稳定性
    // Visit 操作的第二个参数是ID，需要特殊处理
    std::sort(final_schedule.begin(), final_schedule.end());

    for (const auto& op : final_schedule) {
        if (op.type == "Visit") {
            std::cout << op.type << " " << op.time << " " << op.arg1 << "\n";
        } else if (op.type == "Fin") {
            std::cout << op.type << " " << op.time << "\n";
        } else { // Reload or Offload
            std::cout << op.type << " " << op.time << " " << op.arg1 << " " << op.arg2 << "\n";
        }
    }

    return 0;
}
